package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgricultureMaterialInfoVo;
import org.dromara.agriculture.domain.bo.AgricultureMaterialInfoBo;
import org.dromara.agriculture.service.IAgricultureMaterialInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 农资信息
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/materialInfo")
public class AgricultureMaterialInfoController extends BaseController {

    private final IAgricultureMaterialInfoService agricultureMaterialInfoService;

    /**
     * 查询农资信息列表
     */
    @SaCheckPermission("agriculture:materialInfo:list")
    @GetMapping("/list")
    public TableDataInfo<AgricultureMaterialInfoVo> list(AgricultureMaterialInfoBo bo, PageQuery pageQuery) {
        return agricultureMaterialInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出农资信息列表
     */
    @SaCheckPermission("agriculture:materialInfo:export")
    @Log(title = "农资信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgricultureMaterialInfoBo bo, HttpServletResponse response) {
        List<AgricultureMaterialInfoVo> list = agricultureMaterialInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "农资信息", AgricultureMaterialInfoVo.class, response);
    }

    /**
     * 获取农资信息详细信息
     *
     * @param materialId 主键
     */
    @SaCheckPermission("agriculture:materialInfo:query")
    @GetMapping("/{materialId}")
    public R<AgricultureMaterialInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long materialId) {
        return R.ok(agricultureMaterialInfoService.queryById(materialId));
    }

    /**
     * 新增农资信息
     */
    @SaCheckPermission("agriculture:materialInfo:add")
    @Log(title = "农资信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgricultureMaterialInfoBo bo) {
        return toAjax(agricultureMaterialInfoService.insertByBo(bo));
    }

    /**
     * 修改农资信息
     */
    @SaCheckPermission("agriculture:materialInfo:edit")
    @Log(title = "农资信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgricultureMaterialInfoBo bo) {
        return toAjax(agricultureMaterialInfoService.updateByBo(bo));
    }

    /**
     * 删除农资信息
     *
     * @param materialIds 主键串
     */
    @SaCheckPermission("agriculture:materialInfo:remove")
    @Log(title = "农资信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] materialIds) {
        return toAjax(agricultureMaterialInfoService.deleteWithValidByIds(List.of(materialIds), true));
    }
}
